#include <iostream>
#include <vector>
#include <unistd.h>
#include "level_non_zero.h"
#include "mem.h"
#include "sst_table.h"
#include "sst_id.h"
#include "common.h"
#include "entry.h"

int main() {
  std::string dir = "data/L1";
  VirtualR::LevelNonZero *level_non_zero = new VirtualR::LevelNonZero(dir);

  VirtualR::SkipList *mem1 = new VirtualR::SkipList();
  std::string temp = "a_zys_test";
  for (int i = 0; i < 20; i++) {
    mem1->put(temp + std::to_string(i), "hhh" + std::to_string(i));
  }
  // for merge
  mem1->put("zzz", "zzz");

  VirtualR::SkipList *mem2 = new VirtualR::SkipList();
  temp = "b_zys_test";
  for (int i = 0; i < 20; i++) {
    mem2->put(temp + std::to_string(i), "zhhh" + std::to_string(i));
  }

  VirtualR::SkipList *mem3 = new VirtualR::SkipList();
  temp = "c_zys_test";
  for (int i = 0; i < 20; i++) {
    mem3->put(temp + std::to_string(i), "zhhh" + std::to_string(i));
  }

  std::cout << "begin build sst" << std::endl;
  VirtualR::SSTableID id1(dir, 1);
  VirtualR::SSTable table1(mem1, id1);
  uint64_t no = 6;
  std::cout << "begin merge" << std::endl;
  level_non_zero->merge(table1.load(), no);
  std::cout << "end merge" << std::endl;

  VirtualR::SSTableID id2(dir, 2);
  VirtualR::SSTable table2(mem2, id2);
  no = 66;
  std::cout << "begin merge" << std::endl;
  level_non_zero->merge(table2.load(), no);
  std::cout << "end merge" << std::endl;

  VirtualR::SSTableID id3(dir, 3);
  VirtualR::SSTable table3(mem3, id3);
  no = 666;
  std::cout << "begin merge" << std::endl;
  level_non_zero->merge(table3.load(), no);
  std::cout << "end merge" << std::endl;

  table1.remove_file();
  table2.remove_file();
  table3.remove_file();

  level_non_zero->get_index_content_for_debug();
//   std::vector<VirtualR::Entry_t> v = level_non_zero->get_entry();
//   for (auto x : v) {
//     std::cout << x.key << ":" << x.value << std::endl;
//   }

//   v = level_non_zero->get_entry();
//   for (auto x : v) {
//     std::cout << x.key << ":" << x.value << std::endl;
//   }
  level_non_zero->get_index_content_for_debug();

}